<template>
    <el-dialog
        v-model="dialogVisible"
        title="编辑分组信息"
        width="30%"
        :before-close="handClose"
    >
        <div>
            <el-form :model="formData" ref="formRef" label-width="80px" :rules="rules">
                <el-form-item label="分组名称" prop="groupName">
                    <el-input 
                        v-model="formData.groupName" 
                        placeholder="请输入分组名称"
                        :prefix-icon="EditPen"
                    />
                </el-form-item>
                <el-form-item label="排序" prop="mOrder">
                    <el-input 
                        v-model="formData.mOrder" 
                        placeholder="请输入排序, 数值越大越靠前"
                        :prefix-icon="Sort"
                    />
                </el-form-item>
            </el-form>
        </div>
        <template #footer>
        <span class="dialog-footer">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="confirmDialog(formRef)">
            确认
            </el-button>
        </span>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, onMounted,reactive, computed, defineProps, defineEmits, watch} from 'vue'
import {useRouter, useRoute} from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { Sort, EditPen } from '@element-plus/icons-vue'
import EnvApi from '../../../apis/env'
import {useUserStore} from '../../../stores/user'
import { FormValid } from '../../../utils/FormUtils'

const router = useRouter();
const route = useRoute();
const userStore = useUserStore()

const props = defineProps({
    isShow: Boolean,
    handleUpdated: Function,
    data: Object
})

const formRef = ref<FormInstance>()
const formData = ref({
    id: props.data?.id,
    groupName: props.data?.groupName,
    mOrder: props.data?.mOrder
});

const rules = reactive<FormRules>({
    groupName: [
        { required: true, message: '请输入分组名称', trigger: 'blur' },
        { min: 3, max: 32, message: '最小长度3, 最大长度16', trigger: 'blur' },
    ],
})


const emit = defineEmits(['update:isShow']);

const dialogVisible = ref(props.isShow);
watch(()=> props.isShow,() => {
    console.log('props.isShow change', props.isShow);
    dialogVisible.value = props.isShow
});
watch(()=> props.data,() => {
    console.log('props.data change', props.data);
    formData.value.id = props.data?.id;
    formData.value.groupName = props.data?.groupName;
    formData.value.mOrder = props.data?.morder;
});


const handClose = () =>{
    console.log('handClose');
    emit('update:isShow', false)
}

const closeDialog = () => {
    console.log('closeDialog');
    emit('update:isShow', false)
}

const confirmDialog = (formRef: FormInstance | undefined) => {
    if (!formRef) return;
    FormValid(formRef).then(ret=>{
        return EnvApi.updateEnvGroup(formData.value.id, formData.value.groupName, formData.value.mOrder);
    }).then(ret=>{
        if(ret.code == 0){
            ElMessage({
                type: 'success',
                message: "更新成功",
            });
            if(props.handleUpdated){
                props.handleUpdated();
            }
            setTimeout(async () => {
                emit('update:isShow', false)
            }, 1500);
        }else{
            ElMessage({
                type: 'error',
                message: "更新失败",
            });
        }
    }).catch(err=>{
        ElMessage({
            type: 'error',
            message: err
        });
    });
    
}
</script>

<style scoped>
</style>
